package com.alvin.demo.common.config;

import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigInteger;

@Configuration
public class JacksonConfig {
	/**
	 * Jackson全局转化Long类型为String，解决jackson序列化时传入前端Long类型缺失精度问题
	 * long 基本类型的数据不受到影响
	 */
	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
		return jacksonObjectMapperBuilder -> {
			jacksonObjectMapperBuilder.serializerByType(BigInteger.class, ToStringSerializer.instance);
			jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
		};
	}
}
